package com.zrar.award.test.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.zrar.award.test.ExcelUtil;
import com.zrar.award.test.entity.AnswerPeople;
import com.zrar.award.test.entity.Prize;

public class DisplayJPanel extends JPanel implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	List<AnswerPeople> peoples;
	List<Prize> prizes;
	PrizeJFrame prizeJFrame;

	JLabel theme_label = null;
	JLabel file_label = null;
	JTextArea textarea = null;
	JFileChooser fileChooser = null;

	public DisplayJPanel( List<AnswerPeople> peoplelist, List<Prize> prizelist) {
		this.peoples = peoplelist;
		this.prizes = prizelist;
		//this.prizeJFrame = prizeJF;
		prizeJFrame = new PrizeJFrame(prizes);
		
		// 设置面板
		//JPanel contentPane = new JPanel();
		BorderLayout lay=new BorderLayout();
		this.setLayout(lay);
		
		GridLayout gird = new GridLayout(12, 1);
		JPanel set_pan = new JPanel();
		set_pan.setLayout(gird);
		file_label = new JLabel(" ", JLabel.CENTER);
		JButton in_but = new JButton("导入文件");
		JButton set_but = new JButton("设置奖项");
		in_but.addActionListener(this);
		set_but.addActionListener(this);

		set_pan.add(file_label);
		set_pan.add(in_but);
		set_pan.add(set_but);

		// 运行面板
		JPanel run_pan = new JPanel();
		JButton run_but = new JButton("开始");
		JButton stop_but = new JButton("停止");
		run_but.addActionListener(this);
		stop_but.addActionListener(this);
		run_pan.add(run_but);
		run_pan.add(stop_but);

		// 显示面板
		textarea = new JTextArea();
		JScrollPane scrollPane = new JScrollPane(textarea);
		scrollPane.setPreferredSize(new Dimension(350, 300));

		theme_label = new JLabel("抽奖系统", JLabel.CENTER);
		fileChooser = new JFileChooser("D:\\");// 建立一个FileChooser对象,并指定D:的目录为默认文件对话框路径.
		this.add(theme_label, BorderLayout.NORTH);
		this.add(set_pan, BorderLayout.WEST);
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(run_pan, BorderLayout.SOUTH);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		int result;
		File file = null;

		// 导入文件
		if (e.getActionCommand().equals("导入文件")) {
			fileChooser.setApproveButtonText("确定");
			fileChooser.setDialogTitle("打开文件");
			result = fileChooser.showOpenDialog(this);

			if (result == JFileChooser.APPROVE_OPTION) {
				file = fileChooser.getSelectedFile();
				file_label.setText("您导入的文件：" + file.getName());
			} else if (result == JFileChooser.CANCEL_OPTION) {
				file_label.setText("您没有导入任何文件");
			}

			if (file != null) {
				peoples = ExcelUtil.readExcelFile(file);

				if (peoples.size() > 1) {
					for (AnswerPeople p : peoples) {
						textarea.append(p.getPeopleName() + "\r\n");
						textarea.append(p.getPeoTel() + "\r\n");
						textarea.append(p.getPeoCorpType() + "\r\n");
						textarea.append(p.getAddress() + "\r\n");
						textarea.append("=======>" + "\r\n");
					}
				} else {
					System.out.println("没有读取到excel的内容");
				}
			}

		}

		// 设置奖项
		if (e.getActionCommand().equals("设置奖项")) {
			setPrize_Action(e);
		}
		if (e.getActionCommand().equals("开始")) {
			for (Prize prize : prizes) {
				System.out.println(prize.toString());
			}
		}
	}

	public void setPrize_Action(ActionEvent e) {
		prizeJFrame.setVisible(true);
	}

	public static void main(String[] args) {

	}

}
